// pages/my/meal/index.js
const App=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay: 0,
    out: App.Imges.out,
    start: false,
    order: [],
    clsOrderIndex: null,
    limit: 20,
    offset: 1,
    status: '',
  },
  cancel(e) {
    this.setData({
      start: true
    })
  },
  out() {
    this.setData({
      start: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      offset: 1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.offset < (this.data.count / this.data.limit)) {
      this.setData({
        offset: this.data.offset + 1
      })
      this.getList();
    }
  },

  closeOrder(e) {
    console.log('取消订单', e)
    let index = e.detail;
    this.setData({
      clsOrderIndex: index,
      start: true
    })
  },
  yesCloseFun() {
    const item = JSON.parse(JSON.stringify(this.data.order[this.data.clsOrderIndex]));
    let id = item.id;
    let url = 'mp/order/' + id;
    App.ajax(url, 'PUT', '').then(res => {
      if (res.status === 10000) {
        let day = App.util.formatDate(new Date(item.order_time), 'YMD');
        let storage = wx.getStorageSync(day) || {};
        switch (item.top_dishes_type.name){
          case '早餐':
            storage[1] = false;
          break;
          case '中餐':
            storage[2] = false;
          break;
          case '晚餐':
            storage[3] = false;
          break;
        }
        wx.setStorage({
          key: day,
          data: storage
        })
        let order = JSON.parse(JSON.stringify(this.data.order));
        order[this.data.clsOrderIndex].status = -1;
        this.setData({
          order: order,
          start: false
        })
      }
      App.prompts(res.msg);
    }).catch(err => {
      console.log(err)
    })
  },
  getList() {
    let url = 'mp/order/list';
    let param = {
      limit: this.data.limit,
      offset: this.data.offset,
      status: '',
      type: 1
    };
    App.ajax(url, 'GET', param).then(res => {
      if (res.status === 10000) {
        let arr = (res.data.rows).map(val => {
          // let time = (new Date(val.order_time).getTime()) + (1000 * 60 * 60 * 24);
          // let newTime = new Date(time);
          // val.order_time = App.util.formatDate(val.order_time, 'Y-M-D h:s');
          val.time = App.util.formatDate(new Date(val.order_time.replace(/\-/g, "/")), 'Y年M月D日')
          // 只有当天订单可以退
          if (val.time === App.util.formatDate(new Date(), 'Y年M月D日')){
            val.isCancel = true;
          }
          val.dishes_lists.map(v=>{
            v.name = `[${val.top_dishes_type.name}] ${v.name}`;
            return v;
          })
          return val;
        });
        if (this.data.offset === 1) {
          this.setData({
            order: arr,
            count: res.data.count
          })  
        } else {
          let order = JSON.parse(JSON.stringify(this.data.order));
          order.push(arr);
          this.setData({
            order: order,
            count: res.data.count
          })
        }
      }
    }).catch(err => {
      console.log(err)
    })
  },
  delFun(e) {
    console.log('删除', e)
    let index = e.detail;
    let order = JSON.parse(JSON.stringify(this.data.order));
    let url = 'mp/order/' + order[index].id;
    App.ajax(url, 'DELETE', '').then(res => {
      if (res.status === 10000) {
        if (order.length > 5) {
          order.splice(index, 1);
          this.setData({
            order: order
          })
        } else {
          this.getList();
        }
      } else {
        App.prompts(res.msg)
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 自定义方法
  upPay (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      pay: id
    });
  }
})